package com.gl.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.order.entity.TPermission;
import com.gl.order.entity.TRolePermission;
import com.gl.order.entity.dto.PermissionDto;
import com.gl.order.entity.dto.UpdatePermissionDto;
import com.gl.order.mapper.TPermissionMapper;
import com.gl.order.mapper.TRolePermissionMapper;
import com.gl.order.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Objects;

/**
 * @Author: liangSY
 * @Date: 2021/6/9
 * @ClassName: PermissionServiceImpl
 * @Description: PermissionServiceImpl描述
 */
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    TPermissionMapper permissionMapper;

    @Autowired
    TRolePermissionMapper rolePermissionMapper;

    @Override
    public void add(PermissionDto dto) {
        TPermission per = new TPermission();
        BeanUtils.copyProperties(dto,per);
        if(StringUtils.isBlank(dto.getParentId())){
            per.setParentId("0");
        }
        permissionMapper.insert(per);
    }

    @Override
    public List<String> getPermissionByUserId(String userId) {
        List<String> permissionValues = permissionMapper.getPermissionCodeByUserId(userId);
        return permissionValues;
    }

    @Override
    public List<TPermission> list() {
        List<TPermission>  list = permissionMapper.selectList(null);
        return list;
    }

    @Override
    public TPermission findByNameOrCode(String name, String code) {
        QueryWrapper<TPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TPermission::getName,name).or().eq(TPermission::getPermissionCode,code);
        return permissionMapper.selectOne(queryWrapper);
    }

    @Override
    public void updatePermission(UpdatePermissionDto dto) {
        TPermission permission = permissionMapper.selectById(dto.getId());
        if(Objects.isNull(permission)){
            throw new RuntimeException("修改的权限不存在");
        }
        TPermission permission2 = findByNameOrCode(dto.getName(), dto.getPermissionCode());
        if(!Objects.isNull(permission)){
            throw new RuntimeException("重复的权限名或值");
        }
        TPermission per = new TPermission();
        BeanUtils.copyProperties(dto,per);
        dto.setParentId(StringUtils.isBlank(dto.getParentId()) ? "0":dto.getParentId());
        permissionMapper.updateById(per);

    }

    @Override
    public int delPermission(String id) {
        delRolePermissionRef(id);
        return permissionMapper.deleteById(id);
    }

    @Override
    public TPermission findById(String id) {
        return permissionMapper.selectById(id);
    }

    private void delRolePermissionRef(String permissonId){
        rolePermissionMapper.delete(new LambdaQueryWrapper<TRolePermission>().eq(TRolePermission::getPermissionId,permissonId));
    }
}
